﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;

namespace EKG
{

    class QRSxtract
    {
        private ArrayList list = new ArrayList();

        public ArrayList List
        {
            get { return list; }
            set { list = value; }
        }

        public QRSxtract(String path)
        {

            if (!File.Exists(path)) return;
            


            System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
            pProcess.StartInfo.FileName = "c:\\tmp\\ecg\\ecg.exe";
            pProcess.StartInfo.Arguments = path;
            pProcess.StartInfo.UseShellExecute = false;
            pProcess.StartInfo.RedirectStandardOutput = true;
            pProcess.StartInfo.WorkingDirectory = "c:\\tmp\\ecg";
            pProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            pProcess.Start();
            string strOutput = pProcess.StandardOutput.ReadToEnd();
            pProcess.WaitForExit();



            FFactor priv = null;    
            int cnt = 0;
            string line;
            StringReader file = new StringReader(strOutput);
            StreamWriter s =  File.AppendText("c:\\out.txt");
            s.Write(strOutput);
            s.Close();
                while ((line = file.ReadLine()) != null)
                {
                    if (cnt > 9 && line != "" && !line.Contains("mean"))
                    {
                        line = line.Trim();
                        string[] a = line.Split(' ');
                        string prvi = a[0];
                        string time = a[1];
                        String[] b = time.Split(':');
                        String[] b1 = b[2].Split('.');

                        string val = a[4];
                        DateTime sta = new DateTime(1, 1, 1, 0, 0, 0);

                        switch (val)
                        {
                            case "N":
                                priv = new FFactor();
                                priv.Type = "1";
                                priv.N = new DateTime(1, 1, 1, 0, Convert.ToInt32(b[1]), Convert.ToInt32(b1[0]),
                                    Convert.ToInt32(b1[1]));
                                break;
                            case "ECT":
                                priv = new FFactor();
                                priv.Type = "0";
                                priv.N = new DateTime(1, 1, 1, 0, Convert.ToInt32(b[1]), Convert.ToInt32(b1[0]),
                                    Convert.ToInt32(b1[1]));
                                break;
                            case "q":
                                priv.Q = new DateTime(1, 1, 1, 0, Convert.ToInt32(b[1]), Convert.ToInt32(b1[0]),
                                    Convert.ToInt32(b1[1]));
                                break;
                            case "R":
                                priv.R = new DateTime(1, 1, 1, 0, Convert.ToInt32(b[1]), Convert.ToInt32(b1[0]),
                                    Convert.ToInt32(b1[1]));
                                break;
                            case "s":
                                priv.S = new DateTime(1, 1, 1, 0, Convert.ToInt32(b[1]), Convert.ToInt32(b1[0]),
                                    Convert.ToInt32(b1[1]));
                                break;
                            case "(t":
                                priv.T1 = new DateTime(1, 1, 1, 0, Convert.ToInt32(b[1]), Convert.ToInt32(b1[0]),
                                    Convert.ToInt32(b1[1]));
                                break;
                            case "T":
                                priv.T = new DateTime(1, 1, 1, 0, Convert.ToInt32(b[1]), Convert.ToInt32(b1[0]),
                                    Convert.ToInt32(b1[1]));
                                break;
                            case "t)":
                                priv.T2 = new DateTime(1, 1, 1, 0, Convert.ToInt32(b[1]), Convert.ToInt32(b1[0]),
                                    Convert.ToInt32(b1[1]));
                                break;
                            case "P":
                                priv.P = new DateTime(1, 1, 1, 0, Convert.ToInt32(b[1]), Convert.ToInt32(b1[0]),
                                    Convert.ToInt32(b1[1]));

                                priv.PR = priv.P - priv.R;
                            //    if (priv.PR.TotalMilliseconds < 0) priv.PR = new TimeSpan(0, 0, 0, 0, 630);
                                priv.QRS_ST = priv.S - priv.Q;
                              //  if (priv.QRS_ST.TotalMilliseconds < 0) priv.QRS_ST = new TimeSpan(0, 0, 0, 0, 45);
                                priv.QT = priv.T2 - priv.Q;
                              //  if (priv.QT.TotalMilliseconds < 0) priv.QT = new TimeSpan(0,0,0,0, 330);
                                priv.RS = priv.S - priv.R;
                              //  if (priv.RS.TotalMilliseconds < 0) priv.RS = new TimeSpan(0, 0, 0, 0, 17);
                                priv.RT = priv.T - priv.R;
                              //  if (priv.RT.TotalMilliseconds < 0) priv.RT = new TimeSpan(0, 0, 0, 0, 240);
                                priv.ST = priv.T1 - priv.S;
                              //  if (priv.ST.TotalMilliseconds < 0) priv.ST = new TimeSpan(0, 0, 0, 0, 160);


                             /*   if (priv.PR.TotalMilliseconds > 0 && priv.PR.TotalMilliseconds < 1000 &&
                                    priv.QRS_ST.TotalMilliseconds > 0 && priv.QRS_ST.TotalMilliseconds < 1000 &&
                                    priv.QT.TotalMilliseconds > 0 && priv.QT.TotalMilliseconds < 1000 &&
                                    priv.RS.TotalMilliseconds > 0 && priv.RS.TotalMilliseconds < 1000 &&
                                    priv.RT.TotalMilliseconds > 0 && priv.RT.TotalMilliseconds < 1000 &&
                                    priv.ST.TotalMilliseconds > 0 && priv.ST.TotalMilliseconds < 1000)
                              */
                                list.Add(priv);
                                break;
                            default:
                                break;
                        }
                    }
                    else cnt++;
                }

                System.IO.StreamWriter file1 = File.AppendText("c:\\output.txt");
                file1.WriteLine("RS,PR,RT,QT,ST,QRS,AN");
                String toWrite = "";
                foreach (FFactor f in list)
                {
                    toWrite = f.RS.TotalMilliseconds.ToString() + "," + f.PR.TotalMilliseconds.ToString() + "," + f.RT.TotalMilliseconds.ToString() + "," + f.QT.TotalMilliseconds.ToString() + "," + f.ST.TotalMilliseconds.ToString() + "," + f.QRS_ST.TotalMilliseconds.ToString() + "," + f.Type;
                    file1.WriteLine(toWrite);
                }
                file1.Close();
           //     file.Close();
        }
    }

    class FFactor
    {
        string type;
        DateTime p, q, r, s, t2, t, t1, n;
        TimeSpan rs, pr, rt, qt, st, rampli, qrs_qs, rr_hbr;

        public TimeSpan RT
        {
            get { return rt; }
            set { rt = value; }
        }

        public TimeSpan QT
        {
            get { return qt; }
            set { qt = value; }
        }

        public TimeSpan ST
        {
            get { return st; }
            set { st = value; }
        }

        public TimeSpan QRS_ST
        {
            get { return qrs_qs; }
            set { qrs_qs = value; }
        }

        public TimeSpan RS
        {
            get { return rs; }
            set { rs = value; }
        }

        public TimeSpan PR
        {
            get { return pr; }
            set { pr = value; }
        }

        public String Type
        {
            get { return type; }
            set { type = value; }
        }

        public DateTime N
        {
            get { return n; }
            set { n = value; }
        }

        public DateTime P
        {
            get { return p; }
            set { p = value; }
        }

        public DateTime Q
        {
            get { return q; }
            set { q = value; }
        }

        public DateTime R
        {
            get { return r; }
            set { r = value; }
        }


        public DateTime S
        {
            get { return s; }
            set { s = value; }
        }


        public DateTime T
        {
            get { return t; }
            set { t = value; }
        }

        public DateTime T2
        {
            get { return t2; }
            set { t2 = value; }
        }

        public DateTime T1
        {
            get { return t1; }
            set { t1 = value; }
        }
    }
}
